package com.tanhua.sso.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String sendSms(String phone) {
        //String code = RandomUtils.nextInt(100000, 999999) + "";
        String code = "123456";
        //System.out.println(code);
        return code;
    }

    @Override
    public ErrorResult sendCheckCode(String phone) {
        //获取redis中当前手机号对应的验证码
        String redisKey = "CHECK_CODE_" + phone;

        //先判断该手机号发送的验证码是否还未失效
        if(redisTemplate.hasKey(redisKey)){
            String msg = "上一次发送的验证码还未失效！";
            return ErrorResult.builder().errCode("000001").errMessage(msg).build();
        }

//        String code = this.sendSms(phone);
        String code = "123456";

        if(StringUtils.isEmpty(code)){
            String msg = "发送短信验证码失败！";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }

        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        System.out.println(redisKey);
        redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        return null;

    }
/**
 * 发送短信验证码
 * 实现：发送完成短信验证码后，需要将验证码保存到redis中
 * @param phone
 * @return
 *//*
    public ErrorResult sendCheckCode(String phone) {

        String redisKey = "CHECK_CODE_" + phone;

        //先判断该手机号发送的验证码是否还未失效
        if(this.redisTemplate.hasKey(redisKey)){
            String msg = "上一次发送的验证码还未失效！";
            return ErrorResult.builder().errCode("000001").errMessage(msg).build();
        }

//        String code = this.sendSms(phone);
        String code = "123456";

        if(StringUtils.isEmpty(code)){
            String msg = "发送短信验证码失败！";
            return ErrorResult.builder().errCode("000000").errMessage(msg).build();
        }

        //短信发送成功，将验证码保存到redis中，有效期为5分钟
        this.redisTemplate.opsForValue().set(redisKey, code, Duration.ofMinutes(5));

        return null;
    }*/
}
